import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  // state 数据类
  state: {
    footerid: '',
    ishow: false,
    position: {
      lng: '',
      lat: '',
      address: '',
      loaded: false
    },
    main: {
      list: []
    },
    shoplist: {
      list: []
    },
    shop: {
      num: [],
      inside: [],
      obj: {},
      list: []
    },
    params: {
      params: {
        latitude: 22.58856,
        longitude: 113.97447,
        offset: 0,
        limit: 20,
        'extras[]': 'activities',
        keyword: ''
      }
    }
  },
  // mutations 修改state的地方
  mutations: {
    CHANGEFOOT (state, data) {
      state.footerid = data
    },
    MAINSWIPER (state, data) {
      state.main.list = data
    },
    SHOWDETAIL (state, data) {
      state.ishow = !state.ishow
    },
    CHANGECATE (state, data) {
      state.params.params.restaurant_category_ids = data
    },
    CHANGESORT (state, data) {
      state.params.params.order_by = data
    },
    CHANGEMODE (state, data) {
      state.params.params.delivery_mode = data
    },
    CHANGESUPP (state, data) {
      state.params.params.support_ids = data
    },
    ADDKEYWORD (state, data) {
      state.params.params.keyword = data
    },
    SHOPLIST(state, data) {
      // watch: {
      //   position
      // }
      //   ('position', function (newVal, oldVal) {
      //   console.log(newVal)
      // })
      // console.log(state.position.lng)
      // if (state.position.lng) {
      axios.get('http://localhost:3000/shoplist?', state.params
        ).then(res => {
          var reslist = res.data
          var list = []
          for (var i = 0; i < reslist.length; i++) {
            // 距离的数据处理
            var juli = reslist[i].distance > 1000 ? (reslist[i].distance / 1000).toFixed(2) + 'km/' : reslist[i].distance + 'm/'
            var item = {
              juli,
              obj: reslist[i]
            }
            list.push(item)
          }
          state.shoplist.list = list
        })
      // }
    },
    SHOPSEARCH(state, data) {
      axios.get('http://localhost:3000/shopsearch?', state.params
        ).then(res => {
          var reslist = res.data
          var list = []
          for (var i = 0; i < reslist.length; i++) {
            // 距离的数据处理
            var juli = reslist[i].distance > 1000 ? (reslist[i].distance / 1000).toFixed(2) + 'km/' : reslist[i].distance + 'm/'
            var item = {
              juli,
              obj: reslist[i]
            }
            list.push(item)
          }
          state.shoplist.list = list
          // console.log(list)
        })
    },
    SHOP(state, obj) {
      state.shop.obj = obj
    },
    SHOPMENU(state, arr) {
      state.shop.list = arr
    },
    POSITION(state, data) {
      state.position = data;
    },
    SHOPNUM(state, data) {
      state.shop.num = data;
    },
    GETINSIDE(state, index) {
      state.shop.inside = state.shop.num[index].sub_categories
    },
    ADDLIMIT(state, limit) {
      state.params.params.limit = limit
    }
  },
  // getters state 的计算属性
  getters: {

  },
  // actions 操作异步数据  其实就是提交mutations
  actions: {

  }

})
